import menuApi from '@/api/system/menu'
import {menuHeader} from '@/assets/data/menu'
import dataCache from './data-cache'

export async function initSysMenus() {
  var resp = await menuApi.list({type: 'menu'});
  let menuList = resp && resp.data || [];

  let targetTreeList = [], targetAllAliveList = []
  dataIntegration(menuList, targetTreeList, targetAllAliveList);
  targetTreeList = [...menuHeader, ...targetTreeList];

  dataCache.setMenuList(targetAllAliveList);
  dataCache.setMenuTreeList(targetTreeList);
}

/**
 * 获取顶部菜单
 */
export function getHeadMenu() {
  let menuData = [];
  let treeMenus = dataCache.getMenuTreeList();

  //只获取到协同办案子系统
  for (let i = 0; i < treeMenus.length; i++) {
    let item = treeMenus[i];
    if (item.id == 1) {
      menuData = item.children;
      break;
    }
  }
  menuData.map(item => {
    delete item.children;
  });
  return menuData;
}

/**
 * 根据pid查询子菜单
 * @param {*} pid
 */
export function getAsideMenuByPid(pid) {
  let menuList = [];
  if (!pid) {
    return menuList;
  }
  let treeMenus = dataCache.getMenuTreeList();

  //综合执法子系统菜单
  for (let i = 0; i < treeMenus.length; i++) {
    let item = treeMenus[i];
    if (item.id == 1) {
      menuList = item.children;
      break;
    }
  }

  //获取子系统子菜单
  let childMenuList = [];
  for (let i = 0; i < menuList.length; i++) {
    let item = menuList[i];
    if (item.id == pid) {
      childMenuList = item.children;
    }
  }
  return childMenuList;
}

/**
 * 根据路径查询菜单
 * @param {*} path
 * @deprecated：调整：先调用路径查询菜单对象，然后再用菜单id查询子菜单
 */
export function getAsideMenuByPath(path) {
  let menuList = [];
  if (!path) {
    return menuList;
  }

  menuList = dataCache.getMenuList();
  for (let i = 0; i < menuList.length; i++) {
    let item = menuList[i];
    if (path.startsWith(item.path)) {
      menuList = item.children;
    }
  }
  return menuList;
}

/**
 * 根据路径查询菜单对象
 * @param {*} path
 */
export function getMenuByPath(path) {
  let result = {};
  let menuList = dataCache.getMenuList();
  for (let i = 0; i < menuList.length; i++) {
    let item = menuList[i];
    if (item.path == path) {
      result = item;
      break;
    }
  }
  return result;
}

export function getMenuInfoById(id) {
  let result = {};
  let menuList = dataCache.getMenuList();
  for (let i = 0; i < menuList.length; i++) {
    let item = menuList[i];
    if (item.id == id) {
      result = item;
      break;
    }
  }
  return result;
}

function dataIntegration(menuList, treeList, allAliveList) {
  let obj = {};
  menuList.map(el => {
    obj[el.id] = el;
  })
  for (let i = 0, len = menuList.length; i < len; i++) {
    let item = menuList[i];
    if (!item.visible || item.type != 'menu') {
      //无效菜单
      continue;
    }
    let pid = item.pid;
    item.title = item.name;
    item.path = item.url;
    item.icon = item.icon || 'folder-o';

    allAliveList.push(item);

    if (!pid || pid == -1) {
      treeList.push(item);
      continue;
    }
    if (!obj[pid]) {
      continue;
    }
    if (!obj[pid].children) {
      obj[pid].children = [];
    }
    obj[pid].children.push(item);
  }
  sortMenu(treeList);
}

function sortMenu(menuList) {
  menuList = menuList || [];
  menuList.map(menu => {
    sortMenu(menu.children);
    return menu;
  })
  menuList.sort((a, b) => {
    return a.sort > b.sort ? 1 : -1;
  });
}
